package cn.tiasen.helloworld;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.tiasen.helloworld.vo.AppInfo;

import static android.content.pm.PackageManager.MATCH_ALL;

public class AppListActivity extends AppCompatActivity {

    private ListView lv;
    private List<AppInfo> appInfoList;
    private BaseAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_list);

        lv = findViewById(R.id.lv);
        appInfoList = getAllAppinfos();
        adapter = new AppInfoAdatper();
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(),appInfoList.get(position).getName(),Toast.LENGTH_SHORT).show();
            }
        });

        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                appInfoList.remove(position);
                adapter.notifyDataSetChanged();
                return true;
            }
        });
    }



    private class AppInfoAdatper extends BaseAdapter{
        @Override
        public int getCount() {
            return appInfoList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.applist_item, null);

            }
            ImageView iv_icon = convertView.findViewById(R.id.appinfo_icon);
            TextView iv_appname = convertView.findViewById(R.id.appinfo_name);
            iv_icon.setImageDrawable(appInfoList.get(position).getIcon());
            iv_appname.setText(appInfoList.get(position).getName());


            return convertView;
        }
    }


    protected List<AppInfo> getAllAppinfos(){
        List<AppInfo> appinfos = new ArrayList<>();
        // 得到应用packagemanager
        PackageManager packageManager = getPackageManager();

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, MATCH_ALL);
        for(ResolveInfo info : resolveInfos){
            String packageName = info.activityInfo.packageName;
            Drawable icon = info.loadIcon(packageManager);
            String appname = info.loadLabel(packageManager).toString();
            AppInfo appInfo = new AppInfo(appname, packageName, icon);
            appinfos.add(appInfo);


        }
        return appinfos;
    }
}
